/** @format */

import { transformBeijingDate, dateFormatter } from '../../sdk'

// 获取今天的时间
export const getToday = () => {
  console.log(transformBeijingDate(Date.now()))
  return dateFormatter(transformBeijingDate(Date.now()), 'yyyy/MM/dd')
}

// 获取当前年度第X周
export const getYearWeek = () => {
  const today = new Date(getToday())
  const currentYear = today.getFullYear()
  const lastYear = currentYear - 1
  let firstDayOfCurrentYear = new Date(currentYear, 0, 1)
  let firstDayOfLastYear = new Date(lastYear, 0, 1)
  let spendDay = 0
  if (firstDayOfCurrentYear.getDay() != 1) {
    spendDay = 7 - firstDayOfCurrentYear.getDay() + 1
  }
  let lastSpendDay = 0
  if (firstDayOfLastYear.getDay() != 1) {
    lastSpendDay = 7 - firstDayOfLastYear.getDay() + 1
  }
  firstDayOfCurrentYear = new Date(currentYear, 0, 1 + spendDay)
  firstDayOfLastYear = new Date(lastYear, 0, 1 + lastSpendDay)
  const d = Math.ceil((today.valueOf() - firstDayOfCurrentYear.valueOf()) / (24 * 3600 * 1000)) + 1
  const weekNo = Math.ceil(d / 7)
  if (weekNo > 0) {
    return `${currentYear}/${weekNo}`
  }
  const lastD = Math.ceil((today.valueOf() - firstDayOfLastYear.valueOf()) / (24 * 3600 * 1000)) + 1
  return `${lastYear}/${Math.ceil(lastD / 7)}`
}
